//package org.ricks.robot;
//
//import org.ricks.common.lang.Logger;
//import org.ricks.common.utils.ThreadUtils;
//import org.ricks.net.AioSession;
//import org.ricks.net.StateMachineEnum;
//import org.ricks.ioc.TaskBus;
//import org.ricks.net.handler.AbstractMessageProcessor;
//import org.ricks.net.transport.kcp.KcpSession;
//import org.ricks.protocol.ByteBuf;
//import org.ricks.robot.business.*;
//import proto.common.FProto;
//import proto.common.PBCharacter;
//
//import java.util.*;
//import java.util.concurrent.ScheduledFuture;
//import java.util.concurrent.TimeUnit;
//
//public class KcpHandler extends AbstractMessageProcessor<FProto> {
//
//    ScheduledFuture scheduledFuture;
//    @Override
//    public void process0(AioSession aioSession, FProto fProto) {
//        RobotInfo robotInfo = RobotManager.get((KcpSession) aioSession);
//        if(fProto.getKey() == FProto.Key.Push) {
//            Monitor.addActionSuccessPush((KcpSession) aioSession, fProto.getAction() );
//            if(fProto.getAction() == FProto.Action.SyncRecorderData) {
//                Logger.info(RobotManager.get((KcpSession) aioSession).toString() + ": 收到同步信息推送:" + fProto.getAction().toString() );
//
//            }
//            return;
//        }
//
//        switch (fProto.getMsgCode() ) {
//            case -10000,-30000 -> {
//                return;
//            }
//            case -20000 -> {
//                Logger.warn(robotInfo + " -- 警告：压测目标节点主动触发 kcp session close ");
//                aioSession.close();
//                RobotManager.robotMap.remove(aioSession);
////                RobotRoleAction.roleLogout((KcpSession) aioSession);
//                robotInfo.logout = true;
//                return;
//            }
////            case -30000 -> RobotRun.scheduledExecutorService.schedule(() -> RobotAction.startPing((KcpSession) aioSession),2000,TimeUnit.MILLISECONDS);
//            case 200 -> {
//                switch (fProto.getAction()) {
//                    case FProto.Action.Login -> {
////                        RobotManager.get((KcpSession) aioSession).robotId = fProto.getSelf().getPlayerId();
////
////                        int index = robotInfo.robotAccount.lastIndexOf("-") + 1;
////                        int num = Integer.valueOf(robotInfo.robotAccount.substring(index));
////                        if(num > 0 && num % 100 == 0) Monitor.printInfo("机器人" + num + "--");
//////                        Logger.info(RobotManager.get((KcpSession) aioSession).toString() + ": 收到消息模块指令:" + fProto.getAction().toString() +  " ,  请求成功--耗时：" + (System.currentTimeMillis() - startTime) + "ms" );
////                        TaskBus.execute(aioSession, () -> RobotAction.robotStart((KcpSession) aioSession));
//
////                        if(RobotManager.robotMap.size() == RobotRun.robotCount) {
////                            RobotManager.startRun();
////                        }
//
////                        RobotAction.startPing((KcpSession) aioSession);
//
////                        scheduledFuture = RobotRun.scheduledExecutorService.scheduleAtFixedRate(() -> {
////                            ByteBuf buf = new ByteBuf(1);
////                            buf.writeByte((byte) 0x02);
////                            if(!aioSession.isInvalid()) aioSession.send(buf.toArray());
////                        },1000L, 2000L, TimeUnit.MILLISECONDS);
////                        RobotRun.scheduledExecutorService.schedule(() -> RobotRoleAction.roleLogout((KcpSession) aioSession), 20, TimeUnit.SECONDS);
////                        RobotRoleAction.roleLogout((KcpSession) aioSession);
//                        Logger.info("收到登陆成功，，，，，");
//                    }
//                    case Logout -> {
//                        System.err.println(" user logout,,,");
//                        aioSession.close(true);
//                    }
//                }
////                if(fProto.getAction() == FProto.Action.Login) {
////                    RobotManager.get((KcpSession) aioSession).robotId = fProto.getSelf().getPlayerId();
////                    int index = robotInfo.robotAccount.lastIndexOf("-") + 1;
////                    int num = Integer.valueOf(robotInfo.robotAccount.substring(index));
////                    if(num > 0 && num % 100 == 0) Monitor.printInfo("机器人" + num + "--");
////                    Logger.info(RobotManager.get((KcpSession) aioSession).toString() + ": 收到消息模块指令:" + fProto.getAction().toString() +  " ,  请求成功--耗时：" + (System.currentTimeMillis() - startTime) + "ms" );
////                    TaskBus.execute(aioSession, () -> RobotAction.robotStart((KcpSession) aioSession));
////                }
//
//                long startTime = robotInfo.requestTimeMap.getOrDefault(fProto.getAction(), 0L);
//                long costTime = System.currentTimeMillis() - startTime;
//                Monitor.addActionSuccess((KcpSession) aioSession, fProto.getAction(),costTime );
////                Logger.info(RobotManager.get((KcpSession) aioSession).toString() + ": 收到消息模块指令:" + fProto.getAction().toString() +  " ,  请求成功--耗时：" + costTime + "ms" );
//
//
//            }
//            default -> {
////                long startTime = robotInfo.requestTimeMap.getOrDefault(fProto.getAction(), 0L);
////                long costTime = System.currentTimeMillis() - startTime;
////                Monitor.addActionSuccess((KcpSession) aioSession, fProto.getAction(), costTime);
//
//
////                long startTime = robotInfo.requestTimeMap.getOrDefault(fProto.getAction(), 0L);
////                long startTime = fProto.getServerTime();
////                long costTime = System.currentTimeMillis() - startTime;
////                Monitor.addActionSuccess((KcpSession) aioSession, fProto.getAction(), costTime);
////                Logger.info(RobotManager.get((KcpSession) aioSession).toString() + ": 收到消息模块指令:" + fProto.getAction().toString() +  " ,  请求失败--耗时：" + costTime + "ms" + "  , code:" + fProto.getMsgCode() + " 来自机器人：" + fProto.getErrorTips() );
//                Logger.info("收到登陆失败，，，，，");
//            }
//        }
//
//
////        if(fProto.getAction() == FProto.Action.Login && fProto.getMsgCode() == 200) {
////            RobotManager.get((KcpSession) aioSession).robotId = fProto.getSelf().getPlayerId();
////        }
//
////        if(fProto.getKey() == FProto.Key.Push) {
////            Monitor.addActionSuccessPush((KcpSession) aioSession, fProto.getAction() );
////            return;
////        }
////        long startTime = robotInfo.requestTimeMap.getOrDefault(fProto.getAction(), 0L);
////        long costTime = System.currentTimeMillis() - startTime;
////        if(fProto.getMsgCode() == 200) {
//////            Logger.info(RobotManager.get((KcpSession) aioSession).toString() + ": 收到消息模块指令:" + fProto.getAction().toString() +  " ,  请求成功--耗时：" + (System.currentTimeMillis() - startTime) + "ms" );
////
////            Monitor.addActionSuccess((KcpSession) aioSession, fProto.getAction(),costTime );
////        } else {
////            Monitor.addActionSuccess((KcpSession) aioSession, fProto.getAction(), costTime);
//////            Logger.info(RobotManager.get((KcpSession) aioSession).toString() + ": 收到消息模块指令:" + fProto.getAction() +  " ,  请求失败--耗时：" + (System.currentTimeMillis() - startTime) + "ms & 状态code: " + fProto.getMsgCode()  + " , 状态信息：" + fProto.getErrorTips());
////        }
//        if(RobotAction.stating.get()) {
//
//            switch (fProto.getAction()) {
//                case GM -> TaskBus.execute(aioSession, () -> RobotAction.resAddItem((KcpSession) aioSession, fProto));
//                case CookingFood -> TaskBus.execute(aioSession, () -> {
//                    if (RobotLifeAction.startCookTask((KcpSession) aioSession))
//                        RobotLifeAction.startHandicraftTask((KcpSession) aioSession);
//                });
//                case Handicraft -> TaskBus.execute(aioSession, () -> {
//                    if (RobotLifeAction.startHandicraftTask((KcpSession) aioSession))
//                        RobotLifeAction.startSpinTask((KcpSession) aioSession);
//                });
//                case Sewing -> TaskBus.execute(aioSession, () -> {
//                    if (RobotLifeAction.startSpinTask((KcpSession) aioSession)) {
////                        RobotChatAction.robotChatAction((KcpSession) aioSession);
//                        RobotGachaAction.startGachaTask((KcpSession) aioSession);
//                    }
//                });
//                case Chat -> TaskBus.execute(aioSession, () -> {
////                    RobotInfo robotInfo = RobotManager.get((KcpSession) aioSession);
//
//                    if (robotInfo.chatCount < RobotRun.robotChatCount) {
//                        RobotChatAction.robotChatAction((KcpSession) aioSession);
//                    } else {
//                        RobotGachaAction.startGachaTask((KcpSession) aioSession);
//                    }
//                });
//
//                case Gacha -> TaskBus.execute(aioSession, () -> {
////                    RobotInfo robotInfo = RobotManager.get((KcpSession) aioSession);
//                    if (robotInfo.gacha1Count <= RobotRun.robotGacha1Count) {
//                        RobotGachaAction.gacha((KcpSession) aioSession, 1000);
//                    } else if (robotInfo.gacha10Count <= RobotRun.robotGacha10Count)  {
//                        RobotGachaAction.gacha10((KcpSession) aioSession, 1000);
//                    } else  {
//                        if(robotInfo.initCardAction) {
//                            RobotRun.scheduledExecutorService.schedule(() -> RobotStageAction.enterDungeonAction((KcpSession) aioSession, 11101), 1000, TimeUnit.MILLISECONDS);
//
//                        } else {
//                            RobotRoleAction.starRoleAction((KcpSession) aioSession);
//                        }
////                        robotInfo.reset();
////                        RobotLifeAction.startCookTask((KcpSession) aioSession);
//                    }
//                });
//
//                case CharacterUpgrade -> {
////                    RobotInfo robotInfo = RobotManager.get((KcpSession) aioSession);
//                    List<PBCharacter> characterList = fProto.getSelf().getCharactersList();
//                    if(!characterList.isEmpty()) {
//                        int cardId = characterList.get(0).getCharId();
//                        int lv = robotInfo.cardLvMap.getOrDefault(cardId, 1);
//                        if (lv <= 50) {
//                            RobotRoleAction.roleLvUpAction((KcpSession) aioSession, cardId);
//                        } else {
//                            Set<Integer> cardIds = new HashSet<>(RobotRoleAction.roleIds);
//                            cardIds.removeAll(robotInfo.cardLvMap.keySet());
//                            if (!cardIds.isEmpty()) {
//                                RobotRoleAction.roleLvUpAction((KcpSession) aioSession, cardIds.stream().findFirst().get());
//                            } else {
//                                //没有可以升级得角色，开始对角色突破
//                                RobotRoleAction.initRoleStarLvItems((KcpSession) aioSession);
//                            }
//
////                            //没有可以升级得角色，开始对角色突破
////                            RobotRoleAction.initRoleStarLvItems((KcpSession) aioSession);
//                        }
//                    } else if (fProto.getMsgCode() != 200){
//                        //可能是道具不足道具不足，开始对角色突破
//                        RobotRoleAction.initRoleStarLvItems((KcpSession) aioSession);
//                    }
//                }
//                case CharacterStarUp -> {
////                    RobotInfo robotInfo = RobotManager.get((KcpSession) aioSession);
//                    List<PBCharacter> characterList = fProto.getSelf().getCharactersList();
//                    if(!characterList.isEmpty()) {
//                        int cardId = characterList.get(0).getCharId();
//                        int starNum = robotInfo.cardStarMap.getOrDefault(cardId, 1);
//                        if (starNum <= 50) {
//                            RobotRoleAction.roleStarLvAction((KcpSession) aioSession, cardId);
//                        } else {
//                            Set<Integer> cardIds = new HashSet<>(RobotRoleAction.roleIds);
//                            cardIds.removeAll(robotInfo.cardStarMap.keySet());
//                            if (!cardIds.isEmpty()) {
//                                RobotRoleAction.roleStarLvAction((KcpSession) aioSession, cardIds.stream().findFirst().get());
//                            } else {
//                                //没有可以突破得角色，开始进入副本 杀怪
//                                RobotRun.scheduledExecutorService.schedule(() -> RobotStageAction.enterDungeonAction((KcpSession) aioSession, 11101), 1000, TimeUnit.MILLISECONDS);
//                            }
//                        }
////                    //没有可以升级得角色，开始对角色突破
////                    RobotRoleAction.initRoleStarLvItems((KcpSession) aioSession);
//                    } else if (fProto.getMsgCode() != 200){
//                        //可能是道具不足道具不足，进入副本准备杀怪
//                        RobotRun.scheduledExecutorService.schedule(() -> RobotStageAction.enterDungeonAction((KcpSession) aioSession, 11101), 2000, TimeUnit.MILLISECONDS);
//                    }
//                }
//
//                case DungeonEnter ->  RobotRun.scheduledExecutorService.schedule(() -> RobotStageAction.killMonster((KcpSession) aioSession, 1001), 2000, TimeUnit.MILLISECONDS);
//                case MonsterDead -> {
//                    if(robotInfo.killMonsterCount <= RobotRun.robotKillMonsterCount) {
//                        RobotRun.scheduledExecutorService.schedule(() -> RobotStageAction.killMonster((KcpSession) aioSession, 1001), 2000, TimeUnit.MILLISECONDS);
//                    } else {
//                        RobotRun.scheduledExecutorService.schedule(() -> RobotStageAction.collect((KcpSession) aioSession), 2000, TimeUnit.MILLISECONDS);
//                    }
//                }
//                case Collecting -> {
//                    if (robotInfo.collectCount <= RobotRun.robotCollectCount) {
//                        RobotRun.scheduledExecutorService.schedule(() -> RobotStageAction.collect((KcpSession) aioSession), 2000, TimeUnit.MILLISECONDS);
//                    } else {
//                       robotInfo.reset();
//                       RobotRun.scheduledExecutorService.schedule(() -> RobotLifeAction.startCookTask((KcpSession) aioSession),2000,TimeUnit.MILLISECONDS);
//                    }
//                }
//                case ChannelJump -> {
//                    RobotMmoAction.syncFrameData((KcpSession) aioSession);
//                }
//            }
//        }
//
////        if(fProto.getAction() == FProto.Action.CookingFood) {
////            Set<Integer> cookIds = RobotLifeAction.lifeCookItems.keySet();
////            cookIds.removeAll(RobotLifeAction.doCooking);
////            if(CollUtil.isNotEmpty(cookIds)) {
////                int cookId = cookIds.stream().findFirst().get();
////                RobotLifeAction.cooking((KcpSession) aioSession, cookId);
////                RobotLifeAction.doCooking.add(cookId);
////            }
////        }
//
//
//
////        if(fProto.getAction() == FProto.Action.Login && fProto.getMsgCode() == 200) {
////            int index = robotInfo.robotAccount.lastIndexOf("-") + 1;
////            int num = Integer.valueOf(robotInfo.robotAccount.substring(index));
////            if(num > 0 && num % 100 == 0)
////            Monitor.printInfo("机器人" + num + "--");
////            TaskBus.execute(aioSession, () -> RobotAction.robotStart((KcpSession) aioSession));
////        }
//
//    }
//
//    @Override
//    public void stateEvent0(AioSession session, StateMachineEnum stateMachineEnum, Throwable throwable) {
//
//        switch (stateMachineEnum) {
//            case NEW_SESSION: {
////                RobotAction.startPing((KcpSession) session);
//
//                Thread.ofVirtual().start(() -> {
//                    while (true) {
//                        ThreadUtils.sleep(1000L);
//                        ByteBuf buf = new ByteBuf(1);
//                        buf.writeByte((byte) 0x02);
//                        if(!session.isInvalid()) session.send(buf.toArray());
//                    }
//                });
//                Logger.info(" create session......." + session);
//                break;
//            }
//            case PROCESS_EXCEPTION:
//                Logger.error("process exception", throwable);
////                session.close();
//                break;
//            case SESSION_CLOSED:
//                Logger.debug(" close session.......");
//                break;
//            case DECODE_EXCEPTION: {
//                Logger.warn("decode exception,", throwable);
//                break;
//            }
//        }
//
//    }
//
//}
